#include "../include/Monitor.h"
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
Monitor::Monitor(string i)
{
    id = i;
}

Monitor::~Monitor()
{
    //dtor
}
void Monitor::finalize()
{
    findAverage("Tran PPV", "Average TranPeak");
    findMedian("Tran PPV", "Median TranPeak");
    findLowest("Tran PPV", "Lowest TranPeak");
    findHighest("Tran PPV", "Highest TranPeak");

    findAverage("Vert PPV", "Average VertPeak");
    findMedian("Vert PPV", "Median VertP eak");
    findLowest("Vert PPV", "Lowest VertPeak");
    findHighest("Vert PPV", "Highest VertPeak");

    findAverage("Long PPV", "Average LongPeak");
    findMedian("Long PPV", "Median LongPeak");
    findLowest("Long PPV", "Lowest LongPeak");
    findHighest("Long PPV", "Highest LongPeak");
}
void Monitor::findAverage(string setName, string valueName) // Finds the mean of the set of numbers
{
    values[valueName] = 0;
    DataContainer *dc;
    if((dc = getDataContainer(setName)) == NULL) //If the dataContainer does not exist, abort the function
        return;
    for(int i = 0; i < dc->data.size(); i += 1)
    {
        values[valueName] += dc->data[i];
    }
    values[valueName] = values[valueName] / dc->data.size();

}
void Monitor::findMedian(string setName, string valueName)
{
    DataContainer *dc;
      if((dc = getDataContainer(setName)) == NULL) //If the dataContainer does not exist, abort the function
        return;
    //Sorts the vector from lowest to greatest
    vector<float> sortedVector = dc->data;
    sort(sortedVector.begin(), sortedVector.end());

    if(sortedVector.size() & 2 != 0) //If the amount of elements in the vector is odd
        values[valueName] = sortedVector[(ceil( sortedVector.size()/2))];
    else //If the amount of elements in the vector is even
        values[valueName] = sortedVector[(sortedVector.size()/2)] + sortedVector[((sortedVector.size()/2) + 1)];
}
void Monitor::findHighest(string setName, string valueName)
{
    DataContainer *dc;
      if((dc = getDataContainer(setName)) == NULL) //If the dataContainer does not exist, abort the function
        return;
    //Sorts the vector from lowest to greatest
    vector<float> sortedVector = dc->data;
    sort(sortedVector.begin(), sortedVector.end());

    values[valueName] = sortedVector[sortedVector.size()-1];
}
void Monitor::findLowest(string setName, string valueName)
{
    DataContainer *dc;
      if((dc = getDataContainer(setName)) == NULL) //If the dataContainer does not exist, abort the function
        return;
    //Sorts the vector from lowest to greatest
    vector<float> sortedVector = dc->data;
    sort(sortedVector.begin(), sortedVector.end());

    values[valueName] = sortedVector[0];
}
DataContainer* Monitor::getDataContainer(string str)
{
    for(int i = 0; i < dataContainers.size(); i += 1)
    {
        if(dataContainers[i]->name == str)
            return dataContainers[i];
    }
    return NULL;
}
